<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Objects extends CI_Controller {

    function __construct(){
        parent::__construct();
        $_GET['post_type'] = 'objects'; 
        $this->data['page'] = isset($_GET['post_type'])? ucfirst($_GET['post_type']) : ucfirst($_POST['post_type']);
        $this->data['wp_languages'] = array(
        'me' => 'Crnogorski',
        'en' => 'English',
        'ru' => 'Rусский ',
        'fr' => 'Français',
        'al' => 'Albanian'
        ); 
        /*
        * Class Translate
        * set local language
        */
        $this->load->library('translate');
        $this->translate->setLang('me');
        $this->load->model('objects_model');
        $this->load->model('codegen_model');
    }

    function index() { show_404(); }

    function view_edit(){

        $this->data['title'] = "Edit ".ucfirst($_GET['post_type']);      
        $this->data['post_type'] = $_GET['post_type'];     
        $this->data['post_action'] = "EDIT";     
        $this->data['post'] = $this->objects_model->view_edit_posts();
        $this->data['all_posts'] = $this->objects_model->view_all_posts();
        $this->layout->view('posts/edit_resort',$this->data);

    }

    function view_new(){

        $this->load->model('owners_model');

        $this->data['title'] = "Add ".$_GET['post_type'];
        $this->data['post_type'] = $_GET['post_type']; 
        $this->data['post_action'] = "NEW";
        $this->data['owners'] = $this->owners_model->view_all_owners();;
        $this->data['destinations'] = $this->objects_model->view_all_posts('resort');
        
        $this->data['attributes'] = $this->codegen_model->get('attributes','*',"custom = 1",999999);
          $this->firephp->log($this->db->last_query());
        $this->template->build('objects/edit_object',$this->data); 

    }

    function view_all(){

        if(!isset($_GET['post_type']))$_GET['post_type']='resort';

        $this->data['title'] = "View All ".$_GET['post_type'];
        $this->data['post_type'] = $_GET['post_type'];
        $this->data['all_posts'] = $this->objects_model->view_all_objects(); 
        $this->data['fields'] = $this->objects_model->get_fields(); 
        $this->layout->view('objects/view_all_objects',$this->data); 

    }

    function submit_post(){

        switch($_POST['post_action']){

            case 'NEW':     $this->objects_model->create_post();
                break;

            case 'EDIT':    $this->objects_model->update_post();
                break;

        }

    }

    function delete($post_ID){

        if($this->objects_model->delete_post($post_ID)){
            $this->view_all();
        }else{
            echo 'Error ocured';
            $this->view_all();
        }

    }

    public function form_view($type=NULL)
    {   
        
        $this->load->helper('inflector');
        
        $q = 'SELECT oa.ID, oa.type, oa.custom, a.ID as att_ID, a.name, a.value, a.required, a.custom
        FROM object_attributes as oa
        LEFT JOIN attributes as a ON oa.attributes_ID = a.ID
        WHERE oa.type = "'.$_POST['type'].'"
        ';
        
        $this->data['attributes'] = $this->db->query($q)->result_array();
        $data['counter'] = 0;
        $html = '';
        foreach ($this->data['attributes'] as $attribute):
            $data['attribute'] = $attribute;
            $data['post_action'] = "NEW";
            $data['counter']++;
            $html .= $this->load->view('templates/forms/elem_input', $data, TRUE);
        endforeach;
        
        //$this->firephp->log($this->data['attributes']);
        echo json_encode(array('html'=>$html));
    }
    
    public function attribute_view($type)
    {   
        $this->data['post_action'] = "NEW";
        $this->data['post_type'] = $type;   
        echo $this->load->view('objects/attributes/view_attributes',$this->data,TRUE);
    }
    
    public function attribute_new($type)
    {   
        $this->data['post_action'] = "NEW";
        $this->data['post_type'] = $type;   
        echo json_encode($this->load->view('objects/attributes/new_attributes',$this->data,TRUE));
    }

}

    /* End of file Posts.php */
    /* Location: ./application/controllers/Posts.php */